package com.glodon.paas.job.monitor.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.glodon.paas.job.extend.spring.AutowaireHibernateDAOSupport;
import com.glodon.paas.job.model.AlarmConfig;
import com.glodon.paas.job.model.AlarmInfo;

@Repository
public class AlarmDAO extends AutowaireHibernateDAOSupport {

    public void save(AlarmConfig alarmConfig) {
        getHibernateTemplate().saveOrUpdate(alarmConfig);
    }
    public void save(AlarmInfo alarmInfo) {
        getHibernateTemplate().saveOrUpdate(alarmInfo);
    }

    public AlarmConfig getConfig(long id) {
        return getHibernateTemplate().get(AlarmConfig.class, id);
    }
    public List<AlarmConfig> getAllConfig() {
        return getHibernateTemplate().find("from AlarmConfig ac");
    }
    @SuppressWarnings("unchecked")
	public List<AlarmInfo> getAlarmInfo(){
    	 return getHibernateTemplate().find("from AlarmInfo ai where ai.createTime>=(CurDate()-7) and ai.createTime<='DATE_SUB(now() , INTERVAL 1 DAY) 00:00:00' order by ai.updateTime desc");
    }
	
	public List<AlarmInfo> getAlarmInfoNumMinutes(String alarmDetail){
		 List<AlarmInfo> list = new ArrayList<AlarmInfo>();
		 if(StringUtils.isNotBlank(alarmDetail)){
			 Session session = getHibernateTemplate().getSessionFactory().openSession();
			 String sql="SELECT * FROM alarminfo ai where ai.alarmDetail=\""+alarmDetail+"\" and ai.createTime>=DATE_SUB(NOW(), INTERVAL 5 MINUTE) order by ai.updateTime desc ";
			 List<Object[]> resultList = session.createSQLQuery(sql).list();
			 for(Object[] item : resultList){
				 AlarmInfo alarm= new AlarmInfo();
				 alarm.setId(((Number) item[0]).longValue());
				 alarm.setAlarmDetail(String.valueOf(item[1]));
				 alarm.setCreateTime((Date) item[2]);
				 alarm.setNotified((Boolean) item[3]);
				 alarm.setSendTo((String) item[4]);
				 alarm.setTemplateName((String) item[5]);
				 alarm.setTitle((String) item[6]);
				 alarm.setUpdateTime((Date) item[7]);
				 list.add(alarm);
			 }
			 session.close();
		 }
		 return list;
   }
	
    public List<AlarmInfo> getAlarmInfoForPage(int fromNum,int pageNum){
     List<AlarmInfo> list = new ArrayList<AlarmInfo>();
     Session session = getHibernateTemplate().getSessionFactory().openSession();
     String sql="SELECT * FROM alarminfo ai where ai.createTime>=(CurDate()-7) and ai.createTime<='DATE_SUB(now() , INTERVAL 1 DAY) 00:00:00' order by ai.updateTime desc limit "+fromNum+","+pageNum;
     List<Object[]> resultList = session.createSQLQuery(sql).list();
     for(Object[] item : resultList){
    	 AlarmInfo alarm= new AlarmInfo();
    	 alarm.setId(((Number) item[0]).longValue());
    	 alarm.setAlarmDetail(String.valueOf(item[1]));
    	 alarm.setCreateTime((Date) item[2]);
    	 alarm.setNotified((Boolean) item[3]);
    	 alarm.setSendTo((String) item[4]);
    	 alarm.setTemplateName((String) item[5]);
    	 alarm.setTitle((String) item[6]);
    	 alarm.setUpdateTime((Date) item[7]);
    	 list.add(alarm);
     }
     session.close();
     return list;
   }
    public List<AlarmInfo> getUnNotifiedAlarmInfo(){
   	 return getHibernateTemplate().find("from AlarmInfo ai where ai.createTime>=(CurDate()-7) and ai.createTime<='DATE_SUB(now() , INTERVAL 1 DAY) 00:00:00' and ai.notified=0");
   }
}
